//
//  YZSettingVC.m
//  VET
//
//  Created by 刘文靖 on 2018/6/29.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZSettingVC.h"
#import "YZSettingCell.h"
#import "YZSettingItem.h"
#import "YZSecurityVerifyVC.h"
#import "YZNotificationSettingVC.h"
#import "YZPopupController.h"
#import "YZPopupCustomView.h"
#import "YZFeedbackVC.h"
#import "YZUser.h"
#import "YZSignAPI.h"
#import "YZLauncher.h"
@interface YZSettingVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray <YZSettingItem *>*dataSourceArr;
@end

@implementation YZSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
}

- (NSArray<YZSettingItem *> *)dataSourceArr{
    if (!_dataSourceArr) {
        YZSettingItem *item1 = [[YZSettingItem alloc] init];
        item1.optionName = @"密码设置";
        item1.bottomLineHeight = 0.5;
        YZSettingItem *item2 = [[YZSettingItem alloc] init];
        item2.optionName = @"新消息通知";
        item2.bottomLineHeight = 0.5;
        YZSettingItem *item3 = [[YZSettingItem alloc] init];
        item3.optionName = @"反馈";
        item3.bottomLineHeight = 10;
        YZSettingItem *item4 = [[YZSettingItem alloc] init];
        item4.optionName = @"关于兽医助手";
        item4.bottomLineHeight = 0.0;
        _dataSourceArr = @[item1,item2,item3,item4];
    }
    return _dataSourceArr;
}

#pragma mark - event actions

- (IBAction)signOutClicked:(UIButton*)sender {

    // 注销 判断
    sender.enabled = NO;
    YZPopupConfrimCustomView *customView = [[YZPopupConfrimCustomView alloc] init];
    @weakify(self)
    [customView setupUIWithTitle:nil message:@"确定注销？" confirmTitle:@"确定" cancelTitle:@"取消" confirmHandler:^{
        
        // 直接登出
        @strongify(self)
        [self.YZ_popupController dismiss];
        [[YZSignAPI apiSignOut] subscribeNext:^(id  _Nullable x) {
            
            // 清空本地 user
            if([YZUser clearLocalUser]) {
                
                // 跳出到启动页
                [YZLauncher jumpToLaunch];
            }
            
        } error:^(NSError * _Nullable error) {
            
            sender.enabled = YES;
            @strongify(self)
            [self.YZ_popupController dismiss];
        }];
        
    } cancelHandler:^{
        
        sender.enabled = YES;
        @strongify(self)
        [self.YZ_popupController dismiss];
    }];
    [self.YZ_popupController presentContentView:customView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YZSettingCell" forIndexPath:indexPath];
    cell.item = self.dataSourceArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZSettingItem *item = self.dataSourceArr[indexPath.row];
    return item.bottomLineHeight > 1 ?64:54;
}
#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{//密码设置
            YZSecurityVerifyVC *verifyVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZSecurityVerifyVC"];
            [self.navigationController pushViewController:verifyVC animated:YES];
        }break;
        case 1:{//新消息通知
            YZNotificationSettingVC *notificationVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZNotificationSettingVC"];
            [self.navigationController pushViewController:notificationVC animated:YES];
        }break;
        case 2:{//反馈
            YZFeedbackVC *feedbackVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZFeedbackVC"];
            [self.navigationController pushViewController:feedbackVC animated:YES];
        }break;
        case 3:{//关于兽医助手
            
        }break;
            
        default:
            break;
    }
}

@end
